var gulp = require('gulp'),
	gutil = require('gulp-util');
    html2js = require('gulp-html2js'),
    server = require('gulp-express'), //server
    uglify = require('gulp-uglify'), //js minifiy
    rename = require('gulp-rename'), //rename
    cleanCSS = require('gulp-clean-css'), //css minify
    concat = require('gulp-concat'), //concat 合并文件
    imagemin = require('gulp-imagemin'); //image minifiy




//浏览器刷新
var browserSync = require('browser-sync').create();
var reload = browserSync.reload;
// 静态服务器
gulp.task('browser-sync', function() {
    browserSync.init({
        proxy: "localhost:30001"
    });
});

// html2js
gulp.task('html.js', function() {
    gulp.src(['public/views/tpl/**/*.html', 'public/views/pages/**/*.html'])
        .pipe(html2js('templates.js', {
            adapter: 'angular',
            base: 'templates',
            name: 'myApp'
        }))
        .pipe(gulp.dest('public/js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify().on('error',gutil.log))
        .pipe(gulp.dest('public/js'));
});
gulp.watch(['public/views/tpl/**/*.html', 'public/views/pages/**/*.html'], ['html.js']).on("change", reload);

//mini css
gulp.task('css', function() {
    return gulp.src('public/css/style.css')
        .pipe(rename({ suffix: '.min' }))
        .pipe(cleanCSS({ compatibility: 'ie8' }))
        .pipe(gulp.dest('public/css'));
});

//gulp css
gulp.watch('public/css/style.css', ['css']).on("change", reload);

//mini Controller js

gulp.task('Controllerjs', function() {
    return gulp.src('public/views/pages/**/*.js')
        .pipe(concat('controllers.js'))
        .pipe(gulp.dest('public/js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify().on('error',gutil.log))
        .pipe(gulp.dest('public/js'));
});
//gulp Controller js
gulp.watch('public/views/pages/*/*.js', ['Controllerjs']).on("change", reload);

//mini directives js

gulp.task('Tpljs', function() {
    return gulp.src('public/views/tpl/**/*.js')
        .pipe(concat('directives.js'))
        .pipe(gulp.dest('public/js'))
        .pipe(rename({ suffix: '.min' }))
        .pipe(uglify().on('error',gutil.log))
        .pipe(gulp.dest('public/js'));
});
//gulp directives js
gulp.watch(['public/views/tpl/**/**/*.js','public/views/tpl/*/*.js'], ['Tpljs']).on("change", reload);

// mini image
gulp.task('img', function() {
    var format = ['public/images/*', 'public/images/**/*'];
    return gulp.src(format)
        .pipe(imagemin().on('error',gutil.log))
        .pipe(gulp.dest('public/images'));
});

//gulp images

// gulp.watch(['public/images/*.{jpg,png,svg}','public/images/**/*.{jpg,png,svg}'],['img']).on("change", reload);


gulp.task('server', function() {
    // Start the server at the beginning of the task 
    server.run(['./bin/www']);
});
//默认命令，在cmd中输入gulp
gulp.task('default', function() {
    //gulp.start('img');
    gulp.start(['server', 'browser-sync', 'html.js', 'css', 'Controllerjs', 'Tpljs']);
});

gulp.watch(['app.js', 'routes/*.js', 'dao/*.js'], ['server']).on("change", reload);
